/*******************************************************
  File: src/init.h
  Description: Audio Cup LCDd Initialization ( object )
  Author: Brant Anderson
  Date: 10/28/01
  Last Update: 10/29/01
  Status: Working
  To Do: -Read keys from config file
         -Determine size of display
 *******************************************************/



#include <stdio.h>
#include <unistd.h>
#include "../include/init.h"

int sock;

int initDisplay( void ) {
	char buf[8192];

	/** Initialize Display **/
	/** Create Socket **/
	sock = sock_connect( HOST, LCDPORT );
	if( sock <= 0 ) {
		printf( "Could not connect to LCDd server.\n" );
		return 0;
	}
	sock_send_string( sock, "hello\n" );
	usleep( 500000 ); /* wait for the server to say hi */
	sock_recv_string( sock, buf, 8192 );
	printf( "%s\n", buf );

 	/* Name Client ( Acup ) */
	sock_send_string( sock, "client_set -name Acup\n" );
	/* Add Screen ( A ) */
	sock_send_string( sock, "screen_add A\n" );
	/* Name Screen ( Audio Cup ) */
	sock_send_string( sock, "screen_set A -name {Audio Cup}\n" );

	return 1;
}

int keyCheck( int numKeys ) {

	/** TODO: check to make sure the key assignment in
	          the config file is formatted propperly. **/

	return 1;
}


char *initKeypress( int numKeys ) {

 	/** TODO: use config file to define keys **/
 	char *keyPress;
 	char keyAssign[numKeys];

 	char keyUp[20], keyLeft[20], keyDown[20], keyRight[20];
 	char keyMenu[20], keyAction[20], keyDisplay[20], keyMode[20];
	char keyList1[20], keyList2[20], keyList3[20], keyList4[20];

	if( numKeys == 12 ) {

		keyAssign[0] = 'G';
		keyAssign[1] = 'K';
		keyAssign[2] = 'Q';
		keyAssign[3] = 'M';
		keyAssign[4] = 'F';
		keyAssign[5] = 'L';
		keyAssign[6] = 'P';
		keyAssign[7] = 'R';
		keyAssign[8] = 'H';
		keyAssign[9] = 'I';
		keyAssign[10] = 'N';
		keyAssign[11] = 'S';

		keyPress = keyAssign;


		/* Add Keypress ( menu_key(F) ) */
		sprintf( keyMenu, "client_add_key %c\n", keyPress[0] );
		sock_send_string( sock, keyMenu );

		/* Add Keypress ( up_key(G) ) */
		sprintf( keyUp, "client_add_key %c\n", keyPress[1] );
		sock_send_string( sock, keyUp );

		/* Add Keypress ( listone_key(H) ) */
		sprintf( keyList1, "client_add_key %c\n", keyPress[2] );
		sock_send_string( sock, keyList1 );

		/* Add Keypress ( listtwo_key(I) ) */
		sprintf( keyList2, "client_add_key %c\n", keyPress[3] );
		sock_send_string( sock, keyList2 );

		/* Add Keypress ( left_key(K) ) */
		sprintf( keyLeft, "client_add_key %c\n", keyPress[4] );
		sock_send_string( sock, keyLeft );

		/* Add Keypress ( action_key(L) ) */
		sprintf( keyAction, "client_add_key %c\n", keyPress[5] );
		sock_send_string( sock, keyAction );

		/* Add Keypress ( right_key(M) ) */
		sprintf( keyRight, "client_add_key %c\n", keyPress[6] );
		sock_send_string( sock, keyRight );

		/* Add Keypress ( listthree_key(N) ) */
		sprintf( keyList3, "client_add_key %c\n", keyPress[7] );
		sock_send_string( sock, keyList3 );

		/* Add Keypress ( display_key(P) ) */
		sprintf( keyDisplay, "client_add_key %c\n", keyPress[8] );
		sock_send_string( sock, keyDisplay );

		/* Add Keypress ( down_key(Q) ) */
		sprintf( keyDown, "client_add_key %c\n", keyPress[9] );
		sock_send_string( sock, keyDown );

		/* Add Keypress ( mode_key(R) ) */
		sprintf( keyMode, "client_add_key %c\n", keyPress[10] );
		sock_send_string( sock, keyMode );

		/* Add Keypress ( listfour_key(S) ) */
		sprintf( keyList4, "client_add_key %c\n", keyPress[11] );
		sock_send_string( sock, keyList4 );

	}
  	return keyPress;
}

int ac_main( int numKeys ) {

	char *keyPress;

	if( initDisplay( ) == 0 ) return 0;
	if( keyCheck( numKeys ) == 0 ) return 0;
	keyPress = initKeypress( numKeys );
	if( setKeypress( numKeys, keyPress ) == 0 ) return 0;

	msgCodes( sock );

	return 0;
}
